//
//  ACAnchorViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACAnchorViewController.h"
#import "ACAnchorRoomBottomView.h"
#import "ACFeedBackInfoViewController.h"
#import "ACFileManagerViewController.h"
#import "ACAnchorMemberViewController.h"
#import "ACChatAudienceCell.h"
#import "ACAudicenceMicViewController.h"
#import "ACMsgSendView.h"
#import <IQKeyboardManager.h>
#import "ACFileInfoViewController.h"


@interface ACAnchorViewController ()<TRTCLiveRoomDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIImageView *topBgView;
    UIButton *closeBtn;
    
}
@property(nonatomic,strong) UITableView *tableview;
@property(nonatomic,strong)TRTCCloudAnction *trtcAction;
@property(nonatomic,strong)ACMsgSendView *msgSendView;
@property(nonatomic,strong)ACAnchorRoomBottomView *popView;
@property(nonatomic,strong)ACAnchorRoomBottomView *bottomView;
@property (nonatomic, strong)UIView *writeBoardView;
@property (nonatomic, strong)ACEduBoardToolBar *boardToolBar;
@property(nonatomic,assign)BOOL     isMuteVideo;
@property(nonatomic,assign)BOOL     isMuteAudio;
@property(nonatomic,strong)UIButton *anchorOperationBtn;
@property(nonatomic,strong)UILabel  *liveTitle;
@property(nonatomic,copy)NSString *task_id;
@end

@implementation ACAnchorViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 104;
        _tableview.backgroundColor = UIColorFromRGB(0xFFFFFF);
        [_tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACChatAudienceCell" bundle:curB] forCellReuseIdentifier:@"ACChatAudienceCell"];
    }
    return _tableview;
}
-(TRTCCloudAnction *)trtcAction{
    if (!_trtcAction) {
        _trtcAction = [[TRTCCloudAnction alloc] init];
    }
    return _trtcAction;
}
-(NSMutableDictionary<NSString *,TRTCLiveUserInfo *> *)requestJoinAnchor{
    if (!_requestJoinAnchor) {
        _requestJoinAnchor = [[NSMutableDictionary alloc] init];
    }
    return _requestJoinAnchor;
}
-(NSMutableArray<TRTCLiveUserInfo *> *)requestJoinAnchorArray{
    if (!_requestJoinAnchorArray) {
        _requestJoinAnchorArray = [NSMutableArray array];
    }
    return _requestJoinAnchorArray;
}
-(NSMutableArray<TRTCLiveUserInfo *> *)joinAnchorArray{
    if (!_joinAnchorArray) {
        _joinAnchorArray = [NSMutableArray array];
    }
    return _joinAnchorArray;
}
-(ACMsgSendView *)msgSendView{
    if (!_msgSendView) {
        _msgSendView = [[ACMsgSendView alloc] init];
        _msgSendView.isOutScreen = YES;
    }
    return _msgSendView;
}
-(UIView *)writeBoardView{
    if (!_writeBoardView) {
        _writeBoardView = [[UIView alloc] init];
    }
    return _writeBoardView;
}
-(ACEduBoardToolBar *)boardToolBar{
    if (!_boardToolBar) {
       NSArray *titles  = @[@{@"narmal":@"画笔",@"selected":@"画笔"},
        @{@"narmal":@"橡皮",@"selected":@"橡皮"},
        @{@"narmal":@"撤销",@"selected":@"撤销"},@{@"narmal":@"恢复",@"selected":@"恢复"}];
       NSArray *imageNames = @[@{@"narmal":@"icon_live_board_pen",@"selected":@"icon_live_board_pen_selected"},@{@"narmal":@"icon_live_board_eraser",@"selected":@"icon_live_board_eraser_selected"},@{@"narmal":@"icon_live_board_revocation",@"selected":@"icon_live_board_revocation_selected"},@{@"narmal":@"icon_live_board_recover",@"selected":@"icon_live_board_recover_selected"}];
        _boardToolBar = [[ACEduBoardToolBar alloc] initWithTitles:titles imageNameArray:imageNames tagNum:300];
        _boardToolBar.backgroundColor = UIColorFromRGB(0xffffff);
    }
    return _boardToolBar;;
}
-(void)setIsAnchor:(BOOL)isAnchor{
    _isAnchor = isAnchor;
    [self.anchorOperationBtn setHidden:!self.isMC];
}
-(void)setIsMC:(BOOL)isMC{
    _isMC = isMC;
    if (!_isMC) {
        self.backSubject = [RACSubject subject];
    }
    [self setUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[[IQKeyboardManager sharedManager] disabledDistanceHandlingClasses] addObject:self.classForCoder];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[[IQKeyboardManager sharedManager] disabledDistanceHandlingClasses] removeObject:self.classForCoder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    self.fd_interactivePopDisabled = YES;
    self.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 200;
    self.view.backgroundColor = UIColorFromRGB(0xF4F5F8);
    
    self.msgModelArray = [NSMutableArray array];
    self.isMuteAudio = NO;
    self.isMuteVideo = NO;
}
- (void)dealloc
{
    [self.backSubject.rac_willDeallocSignal subscribeCompleted:^{
        DDLogInfo(@"---------rac_willDeallocSignal----%@",self.class);
    }];
}
- (void)showWriteBoardViewWithuserID:(NSString *)userID{
    [self.view insertSubview:self.writeBoardView atIndex:0];
    [self.writeBoardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.top.mas_equalTo(ACStatusBarHeight);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(90);
    }];
    [self.liveRoom startRemoteView:userID View:self.writeBoardView];
}
-(void)showWriteBoardView{
    [SVProgressHUD show];
    [self.liveRoom startShareEduBoardWithUserID:self.me.userId callback:^(int code, NSString * _Nullable message) {
        
    }];
}
-(void)dismissWriteBoardVView{
    [SVProgressHUD show];
    [self.liveRoom endShareEduBoardWithUserID:self.me.userId callback:^(int code, NSString * _Nullable message) {
        
    }];
    [ACLiveRoomManager requestEndPublishWithLiveID:self.liveID task_id:self.task_id requestCallback:^(int code, NSString * _Nullable message) {
        if (code == 0) {
            self.task_id = @"";
        }
        [SVProgressHUD dismiss];
    }];
    [self.writeBoardView removeFromSuperview];
    NSArray *array = [MASViewConstraint installedConstraintsForView:self.writeBoardView];
    for (MASConstraint *constraint in array) {
        [constraint uninstall];
    }
    [self.boardToolBar removeFromSuperview];
    NSArray *array_bar = [MASViewConstraint installedConstraintsForView:self.boardToolBar];
    for (MASConstraint *constraint in array_bar) {
        [constraint uninstall];
    }
    [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo((ACSCREEN_WIDTH * 9.0 / 16.0) + ACStatusBarHeight );
    }];
}
-(void)removeWriteBoardViewWithuserID:(NSString *)userID{
    [self.writeBoardView removeFromSuperview];
    NSArray *array = [MASViewConstraint installedConstraintsForView:self.writeBoardView];
    for (MASConstraint *constraint in array) {
        [constraint uninstall];
    }
    [self.boardToolBar removeFromSuperview];
    NSArray *array_bar = [MASViewConstraint installedConstraintsForView:self.boardToolBar];
    for (MASConstraint *constraint in array_bar) {
        [constraint uninstall];
    }
    [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    [self.liveRoom stopRemoteView:userID];
}
- (void)requestLiveInfo{
    [SVProgressHUD showWithStatus:@""];
    [ACLiveRoomManager requestLiveDetailWithLiveID:self.liveID success:^(id  _Nonnull responseObject) {
        self.liveModel = [ACLiveModel mj_objectWithKeyValues:responseObject];
        self.liveTitle.text = self.liveModel.title;
        [SVProgressHUD dismiss];
    } failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}
-(void)showWriteBoardViewWithBoardView:(UIView *)renderView{
    self.writeBoardView = renderView;
    [self.view insertSubview:self.writeBoardView atIndex:0];
    [self.writeBoardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    
    [self.view addSubview:self.boardToolBar];
    [self.boardToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(54);
        make.top.equalTo(self.writeBoardView.mas_bottom);
    }];
    @weakify(self)
    self.boardToolBar.clickActionHandler = ^(NSInteger index) {
        @strongify(self)
        if (index == 300) {
            // 画笔
            [self.liveRoom setBoardToolTypePen];
        }else if (index == 301){
            // 橡皮
            [self.liveRoom setBoardToolTypeEraser];
        }else if(index == 302){
            [self.liveRoom undoBoard];
        }else if (index == 303){
            [self.liveRoom redoBoard];
        }
    };
    
    [self.videoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.top.mas_equalTo(ACStatusBarHeight);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(90);
    }];
    
    [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo((ACSCREEN_WIDTH * 9.0 / 16.0) + ACStatusBarHeight + 64);
    }];
    [SVProgressHUD dismiss];
    if (self.task_id == nil || [self.task_id isEqualToString:@""]) {
        [ACLiveRoomManager requestStartPublishWithLiveID:self.liveID boardSize:CGSizeMake(100, 100) boardParams:@"" requestCallback:^(int code, NSString * _Nullable message) {
            if (code == 0) {
                self.task_id = message;
            }
        }];
    }
}

-(void)setUI{
    self.videoView = [[UIView alloc] init];
    [self.view addSubview:self.videoView];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ACStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(ACSCREEN_WIDTH * 9.0 / 16.0);
    }];
    NSArray *titles;
    NSArray *imageNames;
//    if (self.isMC) {
//        titles  = @[@{@"narmal":@"发消息",@"selected":@"发消息"},@{@"narmal":@"麦上观众",@"selected":@"麦上观众"},@{@"narmal":@"查看反馈",@"selected":@"查看反馈"},@{@"narmal":@"请求发言",@"selected":@"请求发言"},@{@"narmal":@"更多",@"selected":@"更多"}];
//        imageNames = @[@{@"narmal":@"icon_live_sendMsg",@"selected":@"icon_live_sendMsg"},@{@"narmal":@"icon_live_audience_Mic",@"selected":@"icon_live_audience_Mic"},@{@"narmal":@"icon_live_feedBack",@"selected":@"icon_live_feedBack"},@{@"narmal":@"icon_live_audience_reMic",@"selected":@"icon_live_audience_reMic"},@{@"narmal":@"icon_live_more",@"selected":@"icon_live_more"}];
//    }else{
        titles  = @[@{@"narmal":@"发消息",@"selected":@"发消息"},
        @{@"narmal":@"静      音",@"selected":@"取消静音"},
        @{@"narmal":@"关闭视频",@"selected":@"打开视频"},@{@"narmal":@"镜头翻转",@"selected":@"镜头翻转"},@{@"narmal":@"更多",@"selected":@"更多"}];
        imageNames = @[@{@"narmal":@"icon_live_sendMsg",@"selected":@"icon_live_sendMsg"},@{@"narmal":@"icon_live_voice",@"selected":@"icon_live_muteVoice"},@{@"narmal":@"icon_live_video",@"selected":@"icon_live_muteVideo"},@{@"narmal":@"icon_live_CameraFlip",@"selected":@"icon_live_CameraFlip"},@{@"narmal":@"icon_live_more",@"selected":@"icon_live_more"}];
//    }
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColorFromRGB(0x303133);
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(60 + ACSafeAreaBottomHeight);
    }];
    
    topBgView = [[UIImageView alloc]initWithImage: [UIImage imageWithName:@"icon_live_room_bg" bundle:ACLiteAVUISDK targetClass:[self class]]];
    topBgView.userInteractionEnabled = YES;
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(48 + ACStatusBarHeight);
    }];
    
    self.liveTitle = [[UILabel alloc] init];
    self.liveTitle.text = @"";
    self.liveTitle.textAlignment = NSTextAlignmentCenter;
    self.liveTitle.textColor = UIColorFromRGB(0xffffff);
    self.liveTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [topBgView addSubview:self.liveTitle];
    [self.liveTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(topBgView.mas_bottom).offset(-10);
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-60);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"icon_live_room_close" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(28, 28));
        make.bottom.equalTo(topBgView.mas_bottom).offset(-10);
        make.right.equalTo(topBgView.mas_right).offset(-15);
    }];
    @weakify(self)
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self exitLiveRoom];
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo((ACSCREEN_WIDTH * 9.0 / 16.0) + ACStatusBarHeight + 10);
        make.left.right.mas_equalTo(0);
        make.bottom.equalTo(bgView.mas_top);
    }];
    
    [self.view addSubview:self.msgSendView];
    [self.msgSendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(53);
        make.bottom.mas_offset((53 + ACSafeAreaBottomHeight));
    }];

    self.msgSendView.textHandler = ^(NSString * _Nonnull x) {
        DDLogInfo(@"%@",x);
        @strongify(self)
        //要发送的文本消息
        [self buildMsgModelWithText:x];
    };
    
    self.bottomView = [[ACAnchorRoomBottomView alloc] initWithTitles:titles imageNameArray:imageNames tagNum:100];
    self.bottomView.backgroundColor = UIColorFromRGB(0x303133);
    [bgView addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(-ACSafeAreaBottomHeight);
    }];
    [self.bottomView layoutIfNeeded];
    self.bottomView.clickActionHandler = ^(NSInteger value) {
        @strongify(self)
        switch (value) {
            case 100:{
                [self.msgSendView becameFirstResponder];
            }
                break;
            case 101:{
                self.isMuteAudio = !self.isMuteAudio;
                [self.liveRoom muteLocalAudio:self.isMuteAudio];
            }
                break;
            case 102:{
                self.isMuteVideo = !self.isMuteVideo;
                [self.liveRoom muteLocalVideo:self.isMuteVideo];
            }
                break;
            case 103:{
               // 旋转摄像头方向
                [self.liveRoom switchCamera];
            }
                break;
            case 104:{
                [self setupPopView];
            }
                break;
            default:
                break;
        }
    };
    self.anchorOperationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.anchorOperationBtn setTitle:@"邀请主讲" forState:UIControlStateNormal];
    [self.anchorOperationBtn setTitle:@"踢出主讲" forState:UIControlStateSelected];
    [self.anchorOperationBtn wgk_setBackgroundColor:UIColorFromRGB(0xFF6048) forState:UIControlStateNormal];
    [self.anchorOperationBtn wgk_setBackgroundColor:UIColorFromRGB(0xFF6048) forState:UIControlStateNormal];
    [self.anchorOperationBtn setImage: [UIImage imageWithName:@"icon_live_inviteAnchor" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.anchorOperationBtn setImage: [UIImage imageWithName:@"icon_live_kictOutAnchor" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateSelected];
    [self.anchorOperationBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    self.anchorOperationBtn.layer.masksToBounds = YES;
    self.anchorOperationBtn.layer.cornerRadius = 16;
    self.anchorOperationBtn.layer.shadowColor = [UIColorFromRGB(0xFF6048) colorWithAlphaComponent:0.24].CGColor;
    self.anchorOperationBtn.layer.shadowOffset = CGSizeMake(0,0);
    self.anchorOperationBtn.layer.shadowOpacity = 1;
    self.anchorOperationBtn.layer.shadowRadius = 10;
    [self.anchorOperationBtn wgk_setImagePosition:LXMImagePositionLeft spacing:10];
    [self.anchorOperationBtn setHidden:!self.isMC];
    [self.view addSubview:self.anchorOperationBtn];
    [self.anchorOperationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomView.mas_top).offset(-10);
        make.size.mas_equalTo(CGSizeMake(108, 32));
        make.left.mas_equalTo(15);
    }];
    
    [[self.anchorOperationBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (x.isSelected) {
            // 踢出主讲
            [self.liveRoom kickoutInviteAnchor:@"" callback:^(int code, NSString * _Nullable message) {
                // 恢复按钮的状态为邀请主讲
                 dispatch_async(dispatch_get_main_queue(), ^{
                     [self.anchorOperationBtn setSelected:NO];
                     [self.liveRoom startCameraPreviewWithFrontCamera:YES view:self.videoView callback:^(int code, NSString * _Nullable message) {
                         if (code == 0) {
                             [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
                         }
                     }];
                 });
                 [self.liveRoom notifiAudienceStreamChangedToOwerWithStreamID:@"" callback:^(int code, NSString * _Nullable message) {
                     DDLogInfo(@"通知观众，视频流又切回主持人了");
                 }];
            }];
        }else{
            // 邀请主讲
            ACAnchorMemberViewController *VC = [[ACAnchorMemberViewController alloc] init];
            VC.liveRoom = self.liveRoom;
            [self presentViewController:VC animated:YES completion:nil];
        }
    } completed:^{
        
    }];
}
-(void)setupPopView{
    if (self.popView) {
        [self.popView removeFromSuperview];
        self.popView = nil;
        return ;
    }
    NSArray *titles;
    NSArray *imageNames;
    if (self.isMC) {
        titles  = @[@{@"narmal":@"麦上观众",@"selected":@"麦上观众"},@{@"narmal":@"查看反馈",@"selected":@"查看反馈"},@{@"narmal":@"请求发言",@"selected":@"请求发言"},@{@"narmal":@"文件管理",@"selected":@"文件管理"},@{@"narmal":@"观众",@"selected":@"观众"}];
        imageNames = @[@{@"narmal":@"icon_live_audience_Mic",@"selected":@"icon_live_audience_Mic"},@{@"narmal":@"icon_live_feedBack",@"selected":@"icon_live_feedBack"},@{@"narmal":@"icon_live_audience_reMic",@"selected":@"icon_live_audience_reMic"},@{@"narmal":@"icon_live_fileList",@"selected":@"icon_live_fileList"},@{@"narmal":@"icon_live_audicenList",@"selected":@"icon_live_audicenList"}];
    }else{
        titles  = @[@{@"narmal":@"文件管理",@"selected":@"文件管理"},@{@"narmal":@"观众",@"selected":@"观众"},@{@"narmal":@"共享白板",@"selected":@"结束共享"}];
        imageNames = @[@{@"narmal":@"icon_live_fileList",@"selected":@"icon_live_fileList"},@{@"narmal":@"icon_live_audicenList",@"selected":@"icon_live_audicenList"},@{@"narmal":@"icon_live_shareboard",@"selected":@""}];
    }
    self.popView = [[ACAnchorRoomBottomView alloc] initWithTitles:titles imageNameArray:imageNames tagNum:200];
    self.popView.layer.masksToBounds = YES;
    self.popView.layer.cornerRadius = 4;
    self.popView.backgroundColor = UIColorFromRGB(0x303133);
    [self.view addSubview:self.popView];
    [self.popView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomView.mas_top).offset(-10);
        make.height.mas_equalTo(60);
        if (self.isMC) {
            make.width.mas_equalTo(350);
        }else{
            make.width.mas_equalTo(220);
        }
        make.right.mas_equalTo(-10);
    }];
    [self.popView layoutIfNeeded];
    @weakify(self)
    self.popView.clickActionHandler = ^(NSInteger value) {
        @strongify(self)
        if (!self.isMC) {
            if (value == 200) {
                ACFileManagerViewController *VC = [[ACFileManagerViewController alloc] init];
                VC.index = 1;
                VC.liveID = self.liveID;
                VC.fileManagerSwitchhandler = ^(ACFileModel * _Nonnull fileModel) {
                    if (fileModel) {
                        [self.liveRoom addTranscodeFileWithPages:[fileModel.file_page integerValue] url:fileModel.transcode_url title:fileModel.file_name resolution:@"793x1122"];
                    }
                };
                [self presentViewController:VC animated:YES completion:nil];
            }else if(value == 201){
                ACAnchorMemberViewController *VC = [[ACAnchorMemberViewController alloc] init];
                VC.liveRoom = self.liveRoom;
                [self presentViewController:VC animated:YES completion:nil];
            }else if (value == 202){
                if (self.task_id && self.task_id.length != 0) {
                    // 结束白板
                    [self dismissWriteBoardVView];
                }else{
                    [self showWriteBoardView];
                }
            }
        }else{
            if (value == 200) {
                ACAudicenceMicViewController *VC = [[ACAudicenceMicViewController alloc] init];
                VC.isOnMic = YES;
                VC.joinAnchorArray = self.joinAnchorArray;
                VC.liveRoom = self.liveRoom;
                [self presentViewController:VC animated:YES completion:nil];
                VC.removeUserHandler = ^(TRTCLiveUserInfo * _Nonnull user, BOOL isDel) {
                    [self.joinAnchorArray removeObject:user];
                };
            }else if(value == 201){
                ACFeedBackInfoViewController *VC = [[ACFeedBackInfoViewController alloc] init];
                VC.liveID = self.liveID;
                [self presentViewController:VC animated:YES completion:nil];
            }else if (value == 202){
                ACAudicenceMicViewController *VC = [[ACAudicenceMicViewController alloc] init];
                VC.liveID = self.liveID;
                VC.isOnMic = NO;
                VC.requestJoinAnchorArray = self.requestJoinAnchorArray;
                VC.liveRoom = self.liveRoom;
                [self presentViewController:VC animated:YES completion:nil];
                VC.removeUserHandler = ^(TRTCLiveUserInfo * _Nonnull user, BOOL isDel) {
                    if (!isDel) {
                        [self.joinAnchorArray addObject:user];
                    }
                    [self.requestJoinAnchorArray removeObject:user];
                };
            }else if (value == 203){
                // 查看
                ACFileManagerViewController *VC = [[ACFileManagerViewController alloc] init];
                VC.index = 2;
                VC.liveID = self.liveID;
                VC.fileManagerSwitchhandler = ^(ACFileModel * _Nonnull fileModel) {
                    if (fileModel) {
                        ACFileInfoViewController *fileInfo = [[ACFileInfoViewController alloc] init];
                        fileInfo.id_file = fileModel.id_file;
                        [self.navigationController pushViewController:fileInfo animated:YES];
                    }
                };
                [self presentViewController:VC animated:YES completion:nil];
            }else if (value == 204){
                ACAnchorMemberViewController *VC = [[ACAnchorMemberViewController alloc] init];
                VC.liveRoom = self.liveRoom;
                [self presentViewController:VC animated:YES completion:nil];
            }
        }
    };
}
-(void)exitLiveRoom{
    @weakify(self)
    if (self.isMC) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"如果您不想结束路演，请选择暂时离开" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *exitAction = [UIAlertAction actionWithTitle:@"暂时离开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self)
            [ self.liveRoom exitLiveRoom];
            [self.navigationController popViewControllerAnimated:YES];
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"结束路演" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self)
            [self.navigationController popViewControllerAnimated:YES];
            [self.liveRoom destroyRoom:^(int code, NSString * _Nullable message) {
                DDLogInfo(@"---destroyRoom---code-----%d----message-------%@",code,message);
            }];
            [ACLiveRoomManager requestModityWithLiveID:self.liveID Status:@"0" requestCallback:^(int code, NSString * _Nullable message) {
                if (code == 0) {
                    
                }
            }];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:exitAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else{
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定下麦，成为普通观众" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self)
            [self.liveRoom quitInviteAnchor:self.liveModel.created_by callback:^(int code, NSString * _Nullable message) {
                [self exitInviteAnchor];
            }];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:sureAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}
-(void)exitInviteAnchor{
    @weakify(self)
    if (self.task_id && self.task_id.length != 0) {
        [self dismissWriteBoardVView];
    }
    [self.liveRoom exitRoom:^(int code, NSString * _Nullable message) {
        @strongify(self)
        [self.navigationController popViewControllerAnimated:YES];
        [self.backSubject sendNext:@(1)];
        [self.backSubject sendCompleted];
    }];
}
-(void)receiveInviteAnchorResponseWithInviteAnchor:(TRTCLiveUserInfo *)user agreed:(BOOL)agreed{
    if (!agreed) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您邀请的主讲人已拒绝您的邀请，可重新选择主讲人" preferredStyle:UIAlertControllerStyleAlert];
        @weakify(self)
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
           // 恢复按钮的状态为邀请中
            @strongify(self)
            [self.anchorOperationBtn setSelected:NO];
          
        }];
        [alertVC addAction:cancelAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else{
        [self.anchorOperationBtn setSelected:YES];
        [self.liveRoom stopToAnchor];
    }
}
-(void)receiveInviteAnchorLeave{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您邀请的主讲人已退出，可重新选择主讲人" preferredStyle:UIAlertControllerStyleAlert];
    @weakify(self)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
       // 恢复按钮的状态为邀请主讲
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.anchorOperationBtn setSelected:NO];
            [self.liveRoom startCameraPreviewWithFrontCamera:YES view:self.videoView callback:^(int code, NSString * _Nullable message) {
                if (code == 0) {
                    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
                }
            }];
        });
        [self.liveRoom notifiAudienceStreamChangedToOwerWithStreamID:@"" callback:^(int code, NSString * _Nullable message) {
            DDLogInfo(@"通知观众，视频流又切回主持人了");
        }];
    }];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
-(void)receiveInviteAnchorKickedOut{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已被主持人下麦，可作为观众留着直播间" preferredStyle:UIAlertControllerStyleAlert];
    @weakify(self)
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
        [self exitInviteAnchor];
    }];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
-(void)buildMsgModelWithText:(NSString *)text{
    ACMsgModel *msgModel = [[ACMsgModel alloc] init];
    msgModel.userName = @"我";
    msgModel.userMsg  = text;
    msgModel.userHeadImageUrl = [ACUserProfileManager shareInstance].userModel.avatar;
    [self.liveRoom sendRoomTextMsg:text callback:^(int code, NSString * _Nullable message) {
        
    }];
    [self bulletNewMsg:msgModel];
}
- (void)bulletNewMsg:(ACMsgModel *)msgModel{
    [self.msgModelArray addObject:msgModel];
    [self.tableview reloadData];
}
-(void)dismissPopView{
    if (self.popView) {
        [self.popView removeFromSuperview];
        self.popView = nil;
    }
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.msgModelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACChatAudienceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACChatAudienceCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACChatAudienceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACChatAudienceCell"];
    }
    [cell setupCellContent:self.msgModelArray[indexPath.row]];
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
